package com.atnjupt.dao.impl;

import com.atnjupt.bean.Book;
import com.atnjupt.dao.BookDao;

import java.util.List;

/**
 * @author Wizarder
 * @create 2021-06-04 17:39
 */
public class BookDaoImpl extends BaseDao<Book> implements BookDao {
    @Override
    public int addBook(Book book) {
        String sql ="insert into t_book(name,author,price,sales,stock,img_path) values(?,?,?,?,?,?)";
        int update = update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(),book.getimgPath());
        return update;
    }

    @Override
    public int deletBookById(int id) {
        String sql = "delete from t_book where id = ?";
        int update = update(sql, id);
        return update;
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update t_book set name = ?,author = ?,price = ?,sales = ?,stock = ?,img_path = ? where id = ?";

        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock()
                ,book.getimgPath(),book.getId());
    }

    @Override
    public Book queryBookById(int id) {
        String sql = "select id,name,author,price,sales,stock,img_path from t_book where id = ?";
        return queryForOne(sql,id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql = "select id,name,author,price,sales,stock,img_path from t_book";
        return queryForList(sql);

    }

    @Override
    public int queryPageTotalCount() {
        String sql = "select count(*) from t_book";
        Number o = (Number) queryForSingleValue(sql);
        return o.intValue();
    }

    @Override
    public List<Book> queryForPageItems(int begin, int pageSize) {
        String sql = "select id,name,author,price,sales,stock,img_path from t_book limit ?, ?";
        List<Book> books = queryForList(sql, begin, pageSize);
        return books;
    }

    @Override
    public List<Book> queryForPageItemsByPrince(int begin, int pageSize, int min, int max) {
        String sql = "select id,name,author,price,sales,stock,img_path from t_book where price between ? and ? order by price asc limit ?, ?";
        List<Book> books = queryForList(sql,min,max,begin,pageSize);
        return books;
    }

    @Override
    public Integer queryPageTotalCountByPrice(int min,int max) {
        String sql = "select count(*) from t_book where price between ? and ?";
        Number o = (Number) queryForSingleValue(sql,min,max);
        return o.intValue();
    }
}
